package com.taotao.content.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.taotao.content.service.ContentService;
import com.taotao.jedis.JedisClient;
import com.taotao.mapper.TbContentMapper;
import com.taotao.pojo.EasyUIDataGridResult;
import com.taotao.pojo.TaotaoResult;
import com.taotao.pojo.TbContent;
import com.taotao.pojo.TbContentExample;
import com.taotao.utils.JsonUtils;


@Service
public class ContentServiceImpl implements ContentService {

	@Autowired
	private TbContentMapper contentMapper;
	@Autowired
	private JedisClient jedisClient;
	@Override
	public TaotaoResult addContent(TbContent content) {
		//补全pojo的属性
		content.setCreated( new Date());
		content.setUpdated(new Date());
		//插入到内容表
		contentMapper.insert(content);
		jedisClient.hdel("INDEX_CONTENT", content.getCategoryId()+"");
		return TaotaoResult.ok();
	}

	@Override
	public EasyUIDataGridResult listContent(Long categoryId, int page, int rows) {
		//设置分页信息
		PageHelper.startPage(page,rows);
		EasyUIDataGridResult result=new EasyUIDataGridResult();
		TbContentExample example=new TbContentExample();
		TbContentExample.Criteria criteria=example.createCriteria();
		criteria.andCategoryIdEqualTo(categoryId);
		List<TbContent> contentList=contentMapper.selectByExample(example);
		PageInfo<TbContent> pageInfo=new PageInfo<TbContent>(contentList);
		result.setRows(contentList);
		result.setTotal(pageInfo.getTotal());
		return result;
	}

	@Override
	public TaotaoResult editContent(TbContent content) {
		content.setUpdated(new Date());
		contentMapper.updateByPrimaryKeySelective(content);
		jedisClient.hdel("INDEX_CONTENT", content.getCategoryId()+"");
		return TaotaoResult.ok();
	}

	@Override
	public TaotaoResult deleteContent(String ids) {
		String[] id=ids.split(",");
		for (String string : id) {
			if(!"".equals(string)){
			contentMapper.deleteByPrimaryKey(Long.parseLong(string));
			}
		}
		return TaotaoResult.ok();
	}

	@Override
	public List<TbContent> getContentByCid(Long cid) {
		//先查询缓存
		//添加缓存不能影响正常的业务逻辑
		List<TbContent> contentList=new ArrayList<TbContent>();
		try {
			String json=jedisClient.hget("INDEX_CONTENT", cid+"");
			if(StringUtils.isNotEmpty(json)){
				contentList=JsonUtils.jsonToList(json, TbContent.class);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		//缓存中没有需要查询数据库
		EasyUIDataGridResult result=new EasyUIDataGridResult();
		TbContentExample example=new TbContentExample();
		TbContentExample.Criteria criteria=example.createCriteria();
		criteria.andCategoryIdEqualTo(cid);
		contentList=contentMapper.selectByExample(example);
		//把结果添加到缓存
		try {
			jedisClient.hset("INDEX_CONTENT",cid+"",JsonUtils.objectToJson(contentList));
		} catch (Exception e) {
			e.printStackTrace();
		}
		//返回结果
		return contentList;
	}

}